package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Article;
import com.example.demo.entity.VO.FavoritesVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IArticleService;
import com.example.demo.service.IUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import com.example.demo.service.IFavoritesService;
import com.example.demo.entity.Favorites;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-05
 */
@Api(tags = "文章收藏接口")
@RestController
@RequestMapping("/favorites")
public class FavoritesController {

    @Resource
    private IFavoritesService favoritesService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询收藏表所有数据")
    @GetMapping
    public Result<List<Favorites>> findAll() {
        return Result.success(favoritesService.selectAllFavorites()).msg("查询成功");
    }

    /**
     * 查询指定文章收藏总数接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询指定文章收藏总数接口")
    @GetMapping("/count1")
    public Result<Integer> count1(Integer articleId) {
        if(articleService.findArticleById(articleId) == null){
            return Result.error().msg("查询失败,文章不存在");
        }
        return Result.success(favoritesService.countfavoriteByArticle( articleId)).msg("查询成功");
    }

    /**
     * 查询用户是否收藏文章接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "查询用户收藏文章接口")
    @GetMapping("/findFavoritesByUserAndArticle")
    public Result<Integer> findFavoritesByUserAndArticle(@Valid FavoritesVO favoritesVO) {
        Favorites favorites = new Favorites();
        BeanUtil.copyProperties(favoritesVO,favorites);
        if(userService.getById(favorites.getUserId()) == null){
            return Result.error().msg("查询失败,用户不存在");
        }
        if(articleService.findArticleById(favorites.getArticleId()) == null){
            return Result.error().msg("查询失败,文章不存在");
        }
        List<Favorites> favorites1 = favoritesService.selectFavorites(favorites);
        return Result.success(favorites1).msg("查询成功");
    }

    /**
    * 添加收藏
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "添加收藏")
    @PostMapping("/add")
    public Result<String> add(@RequestBody FavoritesVO favoritesVO) {
        Favorites favorites = new Favorites();
        BeanUtil.copyProperties(favoritesVO,favorites);
        if(userService.getById(favorites.getUserId()) == null){
            return Result.error().msg("添加失败,用户不存在");
        }
        if(articleService.findArticleById(favorites.getArticleId()) == null){
            return Result.error().msg("添加失败,文章不存在");
        }
        List<Favorites> selectFavorites = favoritesService.selectFavorites(favorites);
        if (selectFavorites.size() > 0) {
            return Result.error().msg("添加失败,该用户已经收藏该文章");
        }
        return Result.success(favoritesService.addFavorites(favorites)).msg("添加成功");
    }

    /**
     * 删除接口
     * @param favoritesVO
     * @return
     */
    @ApiOperationSupport(author = "Mr.Yang")
    @ApiOperation(value = "删除接口")
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestBody FavoritesVO favoritesVO) {
        Favorites favorites = new Favorites();
        BeanUtil.copyProperties(favoritesVO,favorites);
        if(userService.getById(favorites.getUserId()) == null){
            return Result.error().msg("删除失败,用户不存在");
        }
        if(articleService.findArticleById(favorites.getArticleId()) == null){
            return Result.error().msg("删除失败,文章不存在");
        }
        return Result.success(favoritesService.deleteFavorites(favorites)).msg("删除成功");
    }
}